﻿using FileSynchronizationClient.Common;
using FileSynchronizationClient.Service;
using Microsoft.Extensions.DependencyInjection;

try
{
    #region Init IOC
    var services = new ServiceCollection();
    // 添加 HttpClient 服务
    services.AddHttpClient();
    services.AddSingleton(typeof(HttpclientHelper));
    services.AddScoped(typeof(FileSynchronizationAuthorization));
    services.AddScoped(typeof(SyncPackHttpClient));
    services.AddScoped(typeof(UserHttpClient));
    services.AddScoped(typeof(CommonHttpClient));
    services.AddScoped(typeof(SettingMaps));

    //获取Token
    services.AddScoped(typeof(UserService));
    // 创建 ServiceProvider
    var serviceProvider = services.BuildServiceProvider();
    var userService = serviceProvider.GetRequiredService<UserService>();
    (var publishType, var token) = await userService.Login();

    services.AddScoped<FileSynchronizationAuthorization>(s =>
    {
        var auth = new FileSynchronizationAuthorization();
        auth.SetAuthorization(token);

        return auth;
    });

    var settingMap = serviceProvider.GetRequiredService<SettingMaps>();

    services.AddScoped<SettingMap>(s => settingMap.GetMap(publishType));
    services.AddScoped(typeof(PublishService));
    #endregion

    // 创建 ServiceProvider
    serviceProvider = services.BuildServiceProvider();

    //获取服务
    var publishService = serviceProvider.GetRequiredService<PublishService>();

    await publishService.PublishAsync();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

Thread.Sleep(1000);
